package com.qidianit.mes.pro.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.common.Result;
import com.qidianit.mes.pojo.pro.WorkFlowProduct;
import com.qidianit.mes.sys.mapper.MaterialMapper;
import com.qidianit.mes.pro.mapper.WorkFlowProductMapper;
import com.qidianit.mes.pro.service.WorkFlowProductService;
import com.qidianit.mes.sys.mapper.UnitMapper;
import com.qidianit.mes.vo.WorkFlowProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Package Path: com.qidianit.mes.pro.service.impl
 * Author: Alhz
 * Description:
 * Date: 2025/9/18 16:44
 */
@Service
public class WorkFlowProductServiceImpl implements WorkFlowProductService {
    @Autowired
    private WorkFlowProductMapper workFlowProductMapper;

    @Autowired
    private MaterialMapper materialMapper;

    @Autowired
    private UnitMapper unitMapper;

    /**
     * 查询工艺流程产品关联列表
     * @param workFlowid
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result selectWorkFlowProductPage(Long workFlowid, int page, int pageSize) {

        PageHelper.startPage(page, pageSize);
        List<WorkFlowProductVo> workFlowProductVoList = workFlowProductMapper.selectWorkFlowProductList(workFlowid);
        PageInfo<WorkFlowProductVo> pageInfo = new PageInfo<>(workFlowProductVoList);

        for (WorkFlowProductVo workFlowProductVo : workFlowProductVoList) {
            workFlowProductVo.setProduct(materialMapper.selectByCode(workFlowProductVo.getProductCode()));

            workFlowProductVo.getProduct().setUnit(unitMapper.selectById(workFlowProductVo.getProduct().getUnitId()));

        }

        return Result.success(pageInfo);

    }


    /**
     * 根据id获取工艺流程产品关联
     * @param id
     * @return
     */ 
    @Override
    public Result getWorkFlowProductById(Long id) {
        return Result.success(workFlowProductMapper.getWorkFlowProductById(id));
    }

    /**
     * 新增工艺流程产品关联
     * @param workFlowProduct
     * @return
     */
    @Override
    public Result addWorkFlowProduct(WorkFlowProduct workFlowProduct) {

        workFlowProduct.setCreateTime(LocalDateTime.now());
        workFlowProduct.setUpdateTime(LocalDateTime.now());

        System.out.println("=============================");
        System.out.println(workFlowProduct);

        // 新增时检查是否已存在相同的关联
        if(workFlowProductMapper.selectWorkFlowProductByWorkFlowIdAndProductCode(workFlowProduct.getWorkflowId(), workFlowProduct.getProductCode())>0){
            return Result.error("工艺流程产品关联已存在");
        }
        System.out.println("=============================");


        return workFlowProductMapper.addWorkFlowProduct(workFlowProduct)>0?Result.success():Result.error("添加失败");
    }

    /**
     * 更新工艺流程产品关联
     * @param workFlowProduct
     * @return
     */
    @Override
    public Result updateWorkFlowProduct(WorkFlowProduct workFlowProduct) {
        System.out.println("=============================");

        System.out.println(workFlowProduct);

        if(workFlowProductMapper.selectWorkFlowProductByWorkFlowIdAndProductCodeExcludeId(workFlowProduct.getWorkflowId(), workFlowProduct.getProductCode(), workFlowProduct.getId())>0){
            return Result.error("工艺流程产品关联已存在");
        }
        System.out.println("=============================");
        return workFlowProductMapper.updateWorkFlowProduct(workFlowProduct)>0?Result.success():Result.error("更新失败");
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Result deleteWorkFlowProductById(Long id) {
        return workFlowProductMapper.deleteWorkFlowProductById(id)>0?Result.success():Result.error("批量删除失败");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public Result batchDeleteWorkFlowProduct(Long[] ids) {
        System.out.println(ids);
        return workFlowProductMapper.batchDeleteWorkFlowProduct(ids)>0?Result.success():Result.error("批量删除失败");
    }


}
